<?php

/*

Process Item Helper (item_helper.pro.php)

*/
ob_start();
$rank_check = 7;
include "global.inc.php";

$find_items2 = mysql_query("SELECT * FROM usersitems2 WHERE owner = '$userid' AND game = '$game'");
while ($find_items = mysql_fetch_array($find_items2))
{
	$id = $find_items[id];

	$findRealItem = fetch("SELECT * FROM items2 WHERE id = '$find_items[item_id]' AND game = '$game'");
	$id2 = $findRealItem[id];

	// STOCK SHOPS

	if ($use[$id] == "stock")
	{
		if ($find_items[parts_left] == $findRealItem[parts])
		{

			$find_shop = fetch("SELECT * FROM usershops2 WHERE owner = '$userid' AND game = 	'$game'");
			$sumItems = fetch("SELECT sum(stock) AS stock FROM usershops_items2 WHERE owner = 	'$userid' AND game = '$game'");

			if ($find_shop[id] == "") { die("$openHTML<p>You do not have a shop yet!$closeHTML"); }

			$max_stock = $find_shop[shop_size] * 5;

			if ($sumItems[stock] >= $max_stock) { die("$openHTML<p>There is not enough room for 	your $find_item2[item_name] in your shop.$closeHTML"); }

			if ($findInShop[stock])
			{
				mysql_query("UPDATE usershops_items2 SET stock=$findInShop[stock]+1 WHERE owner = 	'$userid' AND item_id = '$find_items[item_id]' AND game = '$game'");
			}
			else
			{
				$insert_shopItems .= "('', '$userid', '$find_items[item_id]', '1', '$game'), ";
			}
			mysql_query("DELETE FROM usersitems2 WHERE id = '$find_items[id]' AND owner = '$userid' AND game = '$game'");
		}
		else
		{
			$extra .= "<br>Your $findRealItem[item_name] could not be added because it has already been used.";
		}
	}

	// STOCK GALLERY

	if ($use[$id] == "gallery")
	{
		if ($find_items[parts_left] == $findRealItem[parts])
		{
			$find_gallery = fetch("SELECT * FROM usergallerys WHERE owner = '$userid' AND game = '$game'");
			$sumItems = fetch("SELECT sum(stock) AS stock FROM usergallerys_items WHERE owner = '$userid' AND game = '$game'");

			if ($find_gallery[id] == "") { die("$openHTML<p>You do not have a gallery yet!$closeHTML"); }

			$max_stock = $find_gallery[gallery_size] * 5;

			if ($sumItems[stock] >= $max_stock) { die("$openHTML<p>There is not enough room for your $find_item2[item_name] in your gallery.$closeHTML"); }

			$findIngallery = fetch("SELECT stock FROM usergallerys_items WHERE owner = '$userid' AND item_id = '$find_items[item_id]' AND game = '$game'");

			if ($findIngallery[stock])
			{
				mysql_query("UPDATE usergallerys_items SET stock=$findIngallery[stock]+1 WHERE owner = '$userid' AND item_id = '$find_items[item_id]' AND game = '$game'");
			}
			else
			{
				$insert_galItems .= "('','$userid', '$find_items[item_id]', '1', '$game'), ";
			}

			mysql_query("DELETE FROM usersitems2 WHERE id = '$find_items[id]' AND owner = '$userid' AND game = '$game'");
		}
		else
		{
			$extra .= "<br>Your $findRealItem[item_name] could not be added because it has already been used.";
		}
	}

	// DISCARD ITEM

	if ($use[$id] == "discard")
	{
		mysql_query("DELETE FROM usersitems2 WHERE id = '$find_items[id]' AND owner = '$userid' AND game = '$game'");
	}

	// DONATE ITEM

	if ($use[$id] == "donate")
	{
		$insert_donItems .= "('','$find_items[item_id]', '$userid', '$game'), ";

		mysql_query("DELETE FROM usersitems2 WHERE id = '$find_items[id]' AND owner = '$userid' AND game = '$game'");
	}

	// GIVE ITEM TO FRIEND

	if ($use[$id] == "give")
	{
		$give_to = stripspaces(strtolower($give_to));

		$findGiveTo = fetch("SELECT username,id FROM members2 WHERE username = '$give_to' AND game = '$game'");
		if ($findGiveTo[username])
		{
			$insert_giveItems .= "('', '$findGiveTo[id]', '$find_items[item_id]', '$findRealItem[parts]', '$game'), ";

			mysql_query("DELETE FROM usersitems2 WHERE id = '$find_items[id]' AND owner = '$userid' AND game = '$game'");
			$givenItems++;
		}
	}

	// DEPOSIT ITEM

	if ($use[$id] == "deposit")
	{
		if ($find_items[parts_left] == $findRealItem[parts])
		{
			$findIngallery = fetch("SELECT stock FROM safety_box WHERE owner = '$userid' AND item_id = '$find_items[item_id]' AND game = '$game'");
			if ($findIngallery[stock])
			{
				mysql_query("UPDATE safety_box SET stock=$findIngallery[stock]+1 WHERE owner = '$userid' AND item_id = '$find_items[item_id]' AND game = '$game'");
			}
			else
			{
				$insert_depItems .= "('', '$find_items[item_id]', '$userid', '1', '$game'), ";
			}
			mysql_query("DELETE FROM usersitems2 WHERE id = '$find_items[id]' AND owner = '$userid' AND game = '$game'");
		}
		if ($find_items[parts_left] != $findRealItem[parts])
		{
			$extra .= "<br>Your $findRealItem[item_name] could not be deposited because it has already been used.";
		}
	}
}

if ($insert_shopItems)
{
	$insert_shopItems = substr("$insert_shopItems", 0, -2);
	mysql_query("INSERT INTO usershops_items2 VALUES $insert_shopItems");
}
if ($insert_galItems)
{
	$insert_galItems = substr("$insert_galItems", 0, -2);
	mysql_query("INSERT INTO usergallerys_items VALUES $insert_galItems");
}
if ($insert_donItems)
{
	$insert_donItems = substr("$insert_donItems", 0, -2);
	mysql_query("INSERT INTO penny_store2 VALUES $insert_donItems");
}
if ($insert_giveItems)
{
	$insert_giveItems = substr("$insert_giveItems", 0, -2);
	mysql_query("INSERT INTO usersitems2 VALUES $insert_giveItems");
}
if ($insert_depItems)
{
	$insert_depItems = substr("$insert_depItems", 0, -2);
	mysql_query("INSERT INTO safety_box VALUES $insert_depItems");
}

if (($give_to) AND ($givenItems >= "1"))
{
	systemmessage("$game", "myitems.php",$findGiveTo[id],"$display_name has given you $givenItems items!");
}

header(error("item_helper.php?game=$game","All commands have been executed successfully.$extra"));

?>